package diagram
import "fmt"
type Config struct {
UseAscii bool
ShowCoords bool
Verbose bool
BoxBorderPadding int
PaddingBetweenX int
PaddingBetweenY int
GraphDirection string
StyleType string
SequenceParticipantSpacing int
SequenceMessageSpacing int
SequenceSelfMessageWidth int
}
func DefaultConfig () *Config {
return &Config {
UseAscii : false ,
ShowCoords : false ,
Verbose : false ,
BoxBorderPadding : 1 ,
PaddingBetweenX : 5 ,
PaddingBetweenY : 5 ,
GraphDirection : "LR" ,
StyleType : "cli" ,
SequenceParticipantSpacing : 5 ,
SequenceMessageSpacing : 1 ,
SequenceSelfMessageWidth : 4 ,
}
}
func NewConfig (useAscii bool , graphDirection , styleType string ) (*Config , error ) {
config := &Config {
UseAscii : useAscii ,
ShowCoords : false ,
Verbose : false ,
BoxBorderPadding : 1 ,
PaddingBetweenX : 5 ,
PaddingBetweenY : 5 ,
GraphDirection : graphDirection ,
StyleType : styleType ,
SequenceParticipantSpacing : 5 ,
SequenceMessageSpacing : 1 ,
SequenceSelfMessageWidth : 4 ,
}
if err := config .Validate (); err != nil {
return nil , err
}
return config , nil
}
func NewCLIConfig (useAscii , showCoords , verbose bool , boxBorderPadding , paddingX , paddingY int , graphDirection string ) (*Config , error ) {
defaults := DefaultConfig ()
config := &Config {
UseAscii : useAscii ,
ShowCoords : showCoords ,
Verbose : verbose ,
BoxBorderPadding : boxBorderPadding ,
PaddingBetweenX : paddingX ,
PaddingBetweenY : paddingY ,
GraphDirection : graphDirection ,
StyleType : "cli" ,
SequenceParticipantSpacing : defaults .SequenceParticipantSpacing ,
SequenceMessageSpacing : defaults .SequenceMessageSpacing ,
SequenceSelfMessageWidth : defaults .SequenceSelfMessageWidth ,
}
if err := config .Validate (); err != nil {
return nil , err
}
return config , nil
}
func NewWebConfig (useAscii bool , boxBorderPadding , paddingX , paddingY int ) (*Config , error ) {
defaults := DefaultConfig ()
config := &Config {
UseAscii : useAscii ,
ShowCoords : false ,
Verbose : false ,
BoxBorderPadding : boxBorderPadding ,
PaddingBetweenX : paddingX ,
PaddingBetweenY : paddingY ,
GraphDirection : "LR" ,
StyleType : "html" ,
SequenceParticipantSpacing : defaults .SequenceParticipantSpacing ,
SequenceMessageSpacing : defaults .SequenceMessageSpacing ,
SequenceSelfMessageWidth : defaults .SequenceSelfMessageWidth ,
}
if err := config .Validate (); err != nil {
return nil , err
}
return config , nil
}
func NewTestConfig (useAscii bool , styleType string ) *Config {
config := DefaultConfig ()
config .UseAscii = useAscii
config .StyleType = styleType
return config
}
func (c *Config ) Validate () error {
if c .BoxBorderPadding < 0 {
return &ConfigError {Field : "BoxBorderPadding" , Value : c .BoxBorderPadding , Message : "must be non-negative" }
}
if c .PaddingBetweenX < 0 {
return &ConfigError {Field : "PaddingBetweenX" , Value : c .PaddingBetweenX , Message : "must be non-negative" }
}
if c .PaddingBetweenY < 0 {
return &ConfigError {Field : "PaddingBetweenY" , Value : c .PaddingBetweenY , Message : "must be non-negative" }
}
if c .GraphDirection != "LR" && c .GraphDirection != "TD" {
return &ConfigError {Field : "GraphDirection" , Value : c .GraphDirection , Message : "must be \"LR\" or \"TD\"" }
}
if c .StyleType != "cli" && c .StyleType != "html" {
return &ConfigError {Field : "StyleType" , Value : c .StyleType , Message : "must be \"cli\" or \"html\"" }
}
if c .SequenceParticipantSpacing < 0 {
return &ConfigError {Field : "SequenceParticipantSpacing" , Value : c .SequenceParticipantSpacing , Message : "must be non-negative" }
}
if c .SequenceMessageSpacing < 0 {
return &ConfigError {Field : "SequenceMessageSpacing" , Value : c .SequenceMessageSpacing , Message : "must be non-negative" }
}
if c .SequenceSelfMessageWidth < 2 {
return &ConfigError {Field : "SequenceSelfMessageWidth" , Value : c .SequenceSelfMessageWidth , Message : "must be at least 2" }
}
return nil
}
type ConfigError struct {
Field string
Value interface {}
Message string
}
func (e *ConfigError ) Error () string {
return fmt .Sprintf ("invalid config: %s = %v (%s)" , e .Field , e .Value , e .Message )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .